import sqlite3


app = None
conn = None
c = None

def init(_app):
    global app
    global conn
    global c
    
    app = _app
    
    conn = sqlite3.connect('record.db', check_same_thread = False)
    c = conn.cursor()
    c.execute('''create table if not exists record
        (time integer not null,
        w3name text not null,
        cmd integer not null,
        state integer not null);''')
    conn.commit()
    
def finish():
    conn.close()
    
def open(time, w3name, cmd):
    c.execute("insert into record (time,w3name,cmd,state) values (%s, '%s', %s, 0);" % (time, w3name, cmd));
    conn.commit()
    return True
    
def close(time, w3name, cmd):
    c.execute("update record set state = 1 where time=%s and w3name='%s' and cmd=%s;" % (time, w3name, cmd))
    conn.commit()
    return True